﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace KnowledgeAssistant
{
    class QuestionUtil
    {
        public static List<Question> GetAllQuestions(int test_id)
        {
            List<Question> questions = new List<Question>() { };

            DataTable questiontable = new DataTable();
            List<string> parameters = new List<string>() { test_id.ToString() };
            questiontable = DatabaseConnect.execSP("GetAllQuestions", parameters).Tables[0];

            foreach (DataRow r in questiontable.Rows)
            {
                questions.Add(GetQuestion((int)r["question_id"]));
            }

            return questions;
        }

        public static Question GetQuestion(int question_id)
        {
            Question question = null;

            DataTable questiontable = new DataTable();
            List<string> parameters = new List<string>() { question_id.ToString() };
            questiontable = DatabaseConnect.execSP("GetQuestion", parameters).Tables[0];

            if (questiontable.Rows.Count != 0)
            {
                DataRow q = questiontable.Rows[0];
                Test test = TestUtil.GetTest((int)q["test_id"]);
                //question = new Question((int)q["question_id"], test, q["description"].ToString(), (byte)q["type"]);
            }

            return question;

        }

        public static List<Level> GetListLevels()
        {
            List<Level> levels = new List<Level>();
            DataTable table = DatabaseConnect.execSP("GetLevels").Tables[0];
            foreach (DataRow row in table.Rows)
            {
                levels.Add(
                    new Level(int.Parse(row["level_id"].ToString()),
                        row["name"].ToString(),
                        float.Parse(row["level_value"].ToString())
                        )
                    );
            }
            return levels;
        }

        public static Level GetLevel(int level_id)
        {
            DataTable table = DatabaseConnect.execSP("GetLevel", new List<string>(){level_id.ToString()}).Tables[0];

            Level level = null;

            if (table.Rows.Count != 0)
            {
                level = new Level((int)table.Rows[0]["level_id"],
                            table.Rows[0]["name"].ToString(),
                            float.Parse(table.Rows[0]["level_value"].ToString())
                            );
            }
        
            return level;
        }

        public static Level GetLevel(string name)
        {
            DataTable table = DatabaseConnect.execSP("GetLevelByName", new List<string>() { name }).Tables[0];

            Level level = null;

            if (table.Rows.Count != 0)
            {
                level = new Level(int.Parse(table.Rows[0]["level_id"].ToString()),
                            table.Rows[0]["name"].ToString(),
                            float.Parse(table.Rows[0]["level_value"].ToString())
                            );
            }

            return level;
        }

        public static void UpdateQuestion(Question q)
        {
            List<string> parameters = new List<string>();
            parameters.Add(q.QuestionID.ToString());
            parameters.Add(q.Description);
            parameters.Add(q.Level.LevelID.ToString());
            if (q.Image != null)
            {
                DatabaseConnect.execSP("UpdateThemeQuestion", parameters, q.Image);
            }
            else
            {
                DatabaseConnect.execSP("UpdateThemeQuestion", parameters);
            }
        }

        public static int AddQuestion(Question q)
        {
            int question_id = 0;
            List<string> parameters = new List<string>();
            parameters.Add(q.Theme.ThemeID.ToString());
            parameters.Add(q.Description);
            parameters.Add(q.Level.LevelID.ToString());
            parameters.Add(q.Type.ToString());
            if (q.Image != null)
            {
                question_id = int.Parse(DatabaseConnect.execSP("AddThemeQuestion", parameters, q.Image).Tables[0].Rows[0][0].ToString());
            }
            else 
            {
                question_id = int.Parse(DatabaseConnect.execSP("AddThemeQuestion", parameters).Tables[0].Rows[0][0].ToString());
            }
            return question_id;
        }
    }
}
